#include "../includes/pagoracer.h"

GLfloat modulof (GLfloat a) {
  if (a < 0) {
    return -a;
  };
  return a;
};

GLfloat Ponto2Reta (int i, int x_y) {
  GLfloat x,y,a,b,c,d;
  //PontosDePista[i]
  //  PontosDePista[i].x, PontosDePista[i].y;
  x = PontosDePista[i+1].x - PontosDePista[i].x;
  y = PontosDePista[i+1].y - PontosDePista[i].y;
  a = sqrt(x*x + y*y);
  if (x_y == X) {
    b = y;
    c = PontosDePista[i].x;
  } else {
    b = -x;
    c = PontosDePista[i].y;
  };
  return c + ((DIST_ENTRE_BORDAS_X/a) * b);
}

int DesenhaPista() {
  int i;
  glBegin(GL_TRIANGLE_STRIP);
  for (i=0;i<((NUM_DESENHADOS+1)*NUM_PASSOS);i++) {
    glNormal3f(0.0,0.0,1.0);
    glVertex3f(PontosDePista[i].x, PontosDePista[i].y, PontosDePista[i].z+0.02);
    glNormal3f(0.0,0.0,1.0);
    glVertex3f(Ponto2Reta(i,X),Ponto2Reta(i,Y),PontosDePista[i].z+0.02);
  };
  glEnd();
};

void DesenhaFaixa(){

  int i,j;

  glColor3f(1.0,1.0,1.0);
  for(i=0;i<NUM_FAIXAS;i++){
    glBegin(GL_TRIANGLE_STRIP);
    for(j=0;j<6;j++){
      glVertex3f(PontosDePista[i*DIST_FAIXAS+j].x + DIST_MEIO_BORDA_X - 0.2, 
		 PontosDePista[i*DIST_FAIXAS+j].y,
		 PontosDePista[i*DIST_FAIXAS+j].z);
      glVertex3f(PontosDePista[i*DIST_FAIXAS+j].x + DIST_MEIO_BORDA_X + 0.2, 
		 PontosDePista[i*DIST_FAIXAS+j].y,
		 PontosDePista[i*DIST_FAIXAS+j].z);
    }
    glEnd(); 
  }
}

void DesenhaPlano(GLfloat inix, GLfloat iniy, GLfloat fimx, GLfloat fimy) {
  glBegin(GL_QUADS);
   glNormal3f(0.0,0.0,1.0);
   glVertex3f(inix,iniy,Z);
   glNormal3f(0.0,0.0,1.0);
   glVertex3f(inix,fimy,Z);
   glNormal3f(0.0,0.0,1.0);
   glVertex3f(fimx,fimy,Z);
   glNormal3f(0.0,0.0,1.0);
   glVertex3f(fimx,iniy,Z);
  glEnd();
};

void RenderPagoracer(void) {

  GLfloat Light0_Position[4] = {0.0,0.0,10.0,0.0};
  GLfloat Light1_Position[4] = {-1.0,-1.0,-1.0,0.0};

  /*  GLfloat eye_x; */
  /*  GLfloat center_x; */
  GLfloat x_centro,y_centro;
  int x;

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  //if ((SolAngulo < 90) || (SolAngulo > 270)) {
    glPushMatrix();
    glRotatef(SolAngulo,0.0,1.0,0.0);
    //    glLightfv(GL_LIGHT1,GL_POSITION,Light1_Position);
    //    glEnable(GL_LIGHT1);
    glLightfv(GL_LIGHT0,GL_POSITION,Light0_Position);
    glEnable(GL_LIGHT0);
    glPopMatrix();
    /*  } else {
    glDisable(GL_LIGHT0);
    };*/
  
  glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
  glEnable(GL_COLOR_MATERIAL);

  /*  eye_x = calcula(-0.1*ProfundidadePista,pista[0][0],meio[0][0],pista[1][0]) + 5.0;          */
  /*  center_x = calcula(-0.1*(ProfundidadePista+0.1),pista[0][0],meio[0][0],pista[1][0]) + 5.0; */

  if (Camera == CAMERA_1) {
    gluLookAt(CAMERA_1_EYE_X, CAMERA_1_EYE_Y, CAMERA_1_EYE_Z, 
	      CAMERA_1_CENTER_X, CAMERA_1_CENTER_Y, CAMERA_1_CENTER_Z, 
	      CAMERA_1_UP_X, CAMERA_1_UP_Y, CAMERA_1_UP_Z);
  } else if (Camera == CAMERA_2) {
    if (AnguloAtual < 0.0) {
      x_centro = +(seno*DIST_EYE_CENTER);
    } else {
      x_centro = -(seno*DIST_EYE_CENTER);
    };
    y_centro = cosseno*DIST_EYE_CENTER;
    gluLookAt(CAMERA_2_EYE_X, CAMERA_2_EYE_Y, CAMERA_2_EYE_Z, 
	      CAMERA_2_CENTER_X, CAMERA_2_CENTER_Y, CAMERA_2_CENTER_Z, 
	      CAMERA_2_UP_X, CAMERA_2_UP_Y, CAMERA_2_UP_Z);
  } else if (Camera == CAMERA_3) {
    glRotatef(AnguloAtual,0.0,0.0,1.0);
    gluLookAt(CAMERA_1_EYE_X, -1.0, 2.0, 
	      CAMERA_1_CENTER_X, CAMERA_1_CENTER_Y, CAMERA_1_CENTER_Z, 
	      CAMERA_1_UP_X, CAMERA_1_UP_Y, CAMERA_1_UP_Z);
  };

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glColor3f(GRAMA_RED,GRAMA_GREEN,GRAMA_BLUE);
  DesenhaPlano(PLANO_INICIO_X,PLANO_INICIO_Y,PLANO_FIM_X,PLANO_FIM_Y);
  glPushMatrix();
  glTranslatef(0.0,0.0,-1.0);
  DesenhaPlano(PLANO_INICIO_X-1.0,PLANO_INICIO_Y-1.0,PLANO_FIM_X+1.0,PLANO_FIM_Y+1.0);
  glPopMatrix();
  glTranslatef(0.0,ProfundidadePista,0.0);
  glColor3f(PISTA_RED,PISTA_GREEN,PISTA_BLUE);
  DesenhaPista();
  DesenhaFaixa();
  glPopMatrix();
  glPushMatrix();
  glColor3f(1.0,0.0,0.0);
  glTranslatef(MovCarro,DIST_ORIG_CARRO+1.0,0.0);
  glRotatef(AnguloAtual,ROTA_Z_X,ROTA_Z_Y,ROTA_Z_Z);

  glTranslatef(0.0,0.0,3.0);
  glRotatef(90,1.0,0.0,0.0);
  glRotatef(180,0.0,1.0,0.0);
  //  glTranslatef(0.0,-3.0,3.0);
  glScalef(0.2,0.2,0.2);
  glScalef(0.1,0.1,0.1);
  glmDraw(carro1,GLM_SMOOTH|GLM_MATERIAL);
  
  /*  glColor3f(1.0,0.0,0.0);  
      glmDraw(carro2,GLM_NONE);*/
  glPopMatrix();
  
  //glutSolidCube(1.0);
  glutSwapBuffers();
};
